package pt.isec.android.MyHistory;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DataHelper {
	


	   private static final String DATABASE_NAME = "Myhistory.db";
	   private static final int DATABASE_VERSION = 1;
	   private static final String TABLE_HISTORY = "history";
	   private static final String TABLE_PHONE = "phones";
	
	   private Context context;
	   private SQLiteDatabase db;

	   
	  

	   public DataHelper(Context context) {
	      this.context = context;
	      OpenHelper openHelper = new OpenHelper(this.context);
	      this.db = openHelper.getWritableDatabase();
	    //  this.insertStmt = this.db.compileStatement(INSERT);
	   }
	   public void upgrade()
	   {
		   OpenHelper openHelper = new OpenHelper(this.context);
		   openHelper.onUpgrade(db, DATABASE_VERSION, 1);
		   this.db = openHelper.getWritableDatabase();
	   }

	   public long insertHistory(String type,Integer from, String description, String inout) {
		 
	     
		   //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		   SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
		   Date date = new Date();
		   ContentValues initialValues = new ContentValues(); 
		   initialValues.put("type", type);
		   initialValues.put("data", dateFormat.format(date));
		   initialValues.put("from", from);
		   initialValues.put("description", description);
		   initialValues.put("inout",inout);
		   //long rowId = 

		   
		   return db.insert(TABLE_HISTORY, null, initialValues);
	   }
	   public long insertPhone(Integer phone) {
			 
		     
		   
		   ContentValues initialValues = new ContentValues(); 
		   
		   initialValues.put("phone",phone);
		  

		   
		   return db.insert(TABLE_PHONE, null, initialValues);
	   }
	   public void deleteAllhistory() {
	      this.db.delete(TABLE_HISTORY, null, null);
	      
	   }
	   public void deleteAllphones() {
		      this.db.delete(TABLE_PHONE, null, null);
		      
	   }

	   public List<Integer> selectAllphones() {
	      List<Integer> list = new ArrayList<Integer>();
	      Cursor cursor = this.db.query(TABLE_PHONE, new String[] {"phone"},
	        null, null, null, null, "name desc");
	      if (cursor.moveToFirst()) {
	         do {
	            list.add(cursor.getInt(0));
	         } while (cursor.moveToNext());
	      }
	      if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
	      }
	      return list;
	   }
	   public List<String> selectAllhistory() {
		      List<String> list = new ArrayList<String>();
		      Cursor cursor = this.db.query(TABLE_HISTORY, new String[] {"name","data","type","from","description","inout"},
		        null, null, null, null, "name desc");
		      if (cursor.moveToFirst()) {
		         do {
		            list.add(cursor.getString(0)+" "+cursor.getString(1)+" "+
		            		cursor.getString(2)+""+cursor.getString(3)+" "+
		            		cursor.getString(4)+" "+cursor.getString(5));
		         } while (cursor.moveToNext());
		      }
		      if (cursor != null && !cursor.isClosed()) {
		         cursor.close();
		      }
		      return list;
		   }

	   private static class OpenHelper extends SQLiteOpenHelper {

	      OpenHelper(Context context) {
	         super(context, DATABASE_NAME, null, DATABASE_VERSION);
	      }

	      @Override
	      public void onCreate(SQLiteDatabase db) {
	         db.execSQL("CREATE TABLE " + TABLE_HISTORY + "(id INTEGER PRIMARY KEY, type TEXT, data TEXT, from NUMERIC, description TEXT,inout TEXT)");
	         db.execSQL("CREATE TABLE " + TABLE_PHONE + "(id INTEGER PRIMARY KEY, phone numeric)");
	     
	      }

	      @Override
	      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
	         db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
	         db.execSQL("DROP TABLE IF EXISTS " + TABLE_PHONE);
	         onCreate(db);
	      }
	   }
	

}
